//
//  LoginViewController.m
//  Bees
//
//  Created by Xiaohan on 2018/12/3.
//  Copyright © 2018 xiaohan. All rights reserved.
//

#import "LoginViewController.h"
#import "UnLineTextField.h"
#import "RegisterViewController.h"
#import "BackUpBeesKeyViewController.h"
#import "UserServer.h"
#import "FoundBackMineDataViewController.h"
@interface LoginViewController ()<UITextFieldDelegate>
@property (nonatomic,strong) UnLineTextField *phoneTF;
@property (nonatomic,strong) UnLineTextField *pwdTF;
@property (nonatomic,strong) XThemeFlatButton *loginBtn;
@property (nonatomic,strong) UserServer*userServer;

@end
@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}
- (void)setupUI{
    self.isShowLeftBack = NO;
    self.customNavigationBar.height = kStatusBarHeight+AdaptedWidth(80);
    UILabel *tipLab = [UILabel new];
    tipLab.textColor = UIColorHex(#000000);
    tipLab.font = kBoldFont(30);
    tipLab.text = XLocalized(@"登录");
    [self.customNavigationBar addSubview:tipLab];
    [tipLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(AdaptedWidth(20));
        make.bottom.mas_equalTo(0);
    }];
    
    UIButton *cancel = [UIButton new];
    [cancel setTitle:XLocalized(@"取消") forState:UIControlStateNormal];
    [cancel setTitleColor:UIColorHex(333333) forState:UIControlStateNormal];
    [self.customNavigationBar addSubview:cancel];
    [cancel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(AdaptedWidth(-20));
        make.centerY.equalTo(tipLab.mas_centerY);
        make.height.mas_equalTo(35);
    }];
    kWeakSelf(self);
    [cancel addTapBlock:^(UIButton * _Nonnull btn) {
        !weakself.CancelBlock?:weakself.CancelBlock();
        [weakself backBtnClicked];
    }];
    
    [self.contentView addSubview:self.phoneTF];
    [self.phoneTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(AdaptedWidth(18));
        make.right.mas_equalTo(AdaptedWidth(-18));
        make.top.mas_equalTo(self.contentView.mas_top).offset(AdaptedHeight(40));
        make.height.mas_equalTo(AdaptedWidth(40));
    }];
    [self.contentView addSubview:self.pwdTF];
    [self.pwdTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(AdaptedWidth(18));
        make.right.mas_equalTo(-AdaptedWidth(18));
        make.top.mas_equalTo(self.phoneTF.mas_bottom).offset(AdaptedHeight(40));
        make.height.mas_equalTo(AdaptedWidth(40));
    }];
    [self.contentView addSubview:self.loginBtn];
    [self.loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(AdaptedWidth(18));
        make.right.mas_equalTo(AdaptedWidth(-18));
        make.top.mas_equalTo(self.pwdTF.mas_bottom).offset(AdaptedHeight(56));
        make.height.mas_equalTo(AdaptedWidth(40));
    }];
    
    [self.loginBtn addTapBlock:^(UIButton * _Nonnull btn) {
        [weakself.view endEditing:YES];
        if (!weakself.phoneTF.text.length) {
            [weakself.view makeToast:weakself.phoneTF.placeholder];
            return;
        }
        if (!weakself.pwdTF.text.length) {
            [weakself.view makeToast:weakself.pwdTF.placeholder];
            return;
        }
        weakself.userServer.password = weakself.pwdTF.text;
        weakself.userServer.account = weakself.phoneTF.text;
        [MBProgressHUD showLoadTitile:@"" ToView:weakself.view];
        [weakself.userServer loginComplete:^(BOOL success, NSString * _Nonnull msg) {
            [MBProgressHUD hideHUDForView:weakself.view];
            if (success) {
                SAVELOCAL(weakself.phoneTF.text, @"lastAccount");
                [MBProgressHUD showTopTipMessage:msg isWindow:YES];
                !weakself.LoginSuccess?:weakself.LoginSuccess();
                [weakself.navigationController popViewControllerAnimated:YES];
            }else{
                [weakself.view makeToast:msg];
            }
        }];
    }];
    
    UIButton *forgetBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [forgetBtn setTitle:XLocalized(@"忘记密码?") forState:UIControlStateNormal];
    [forgetBtn setTitleColor:kColorMain forState:UIControlStateNormal];
    forgetBtn.titleLabel.font = BOLDSYSTEMFONT(15);
    [forgetBtn addTapBlock:^(UIButton * _Nonnull btn) {
        FoundBackMineDataViewController *vc = [FoundBackMineDataViewController new];
        [weakself.navigationController pushViewController:vc animated:YES];
    }];
    [self.contentView addSubview:forgetBtn];
    [forgetBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.equalTo(self.loginBtn.mas_bottom).offset(AdaptedHeight(25));
    }];
    
    YYLabel *bottomLab = [YYLabel new];
    NSString *str1 = XLocalized(@"还没有Bees账号? ");
    NSString *str2 = XLocalized(@"注册");
    NSString* bottomText = [NSString stringWithFormat:@"%@%@",str1,str2];
    NSRange range = [bottomText rangeOfString:str2];
    NSMutableAttributedString *text  = [[NSMutableAttributedString alloc] initWithString:bottomText];
    [text setFont:kBoldFont(15) range:[text rangeOfAll]];
    text.color = UIColorHex(#B5B5B5);
    [text setTextHighlightRange:range color:UIColorHex(000000) backgroundColor:[UIColor clearColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        RegisterViewController *vc = [RegisterViewController new];
        vc.RegisterSuccessBlock = ^(UserInfo * _Nonnull info) {
            BackUpBeesKeyViewController *vc = [BackUpBeesKeyViewController new];
            vc.info = info;
            [weakself.navigationController popViewControllerAnimated:NO];
            [weakself.navigationController pushViewController:vc animated:YES];
        };
        [weakself.navigationController pushViewController:vc animated:YES];
    }];
    bottomLab.attributedText = text;
    [self.contentView addSubview:bottomLab];
    [bottomLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(forgetBtn.mas_bottom).offset(AdaptedHeight(190));
        make.centerX.mas_equalTo(0);
        make.bottom.mas_equalTo(AdaptedHeight(-46));
    }];
    
//    if ([UIDevice currentDevice].isSimulator) {
//        self.phoneTF.text = @"27167107";
//        self.pwdTF.text = @"a12345678";
//    }else if(kIsIphoneX){
//        self.phoneTF.text = @"58698163";
//        self.pwdTF.text = @"a12345678";
//    }
    if (VALUEFORKEY(@"lastAccount")) {
        self.phoneTF.text = VALUEFORKEY(@"lastAccount");
    }
}
#pragma mark - textField delegate

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}
#pragma mark - lazy
- (UnLineTextField *)phoneTF{
    if (!_phoneTF) {
        _phoneTF = [UnLineTextField new];
        _phoneTF.placeholder = @"请输入账号";
        _phoneTF.delegate = self;
        _phoneTF.font = kDefaultFont(15);
        _phoneTF.textColor = UIColorHex(333333);
        _phoneTF.clearButtonMode = UITextFieldViewModeAlways;
        _phoneTF.keyboardType = UIKeyboardTypeNumberPad;
        _phoneTF.returnKeyType = UIReturnKeyDone;
        UIButton *clearButton = [_phoneTF valueForKey:@"_clearButton"];
        [clearButton setImage:[UIImage imageNamed:@"tf_delete"] forState:UIControlStateNormal];
    }
    return _phoneTF;
}

- (UnLineTextField*)pwdTF{
    if (!_pwdTF) {
        _pwdTF = [UnLineTextField new];
        _pwdTF.placeholder = @"请输入密码";
        _pwdTF.font = kDefaultFont(15);
        _pwdTF.delegate = self;
        _pwdTF.textColor = UIColorHex(333333);
        UIButton *eyeBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
        kWeakSelf(eyeBtn);
        kWeakSelf(self);
        [eyeBtn addTapBlock:^(UIButton * _Nonnull btn) {
            weakeyeBtn.selected = !weakeyeBtn.selected;
            if (weakeyeBtn.selected) {
                weakself.pwdTF.secureTextEntry = NO;
            }else{
                weakself.pwdTF.secureTextEntry = YES;
            }
        }];
        _pwdTF.secureTextEntry = YES;
        [eyeBtn setImage:[UIImage imageNamed:@"close_eye"] forState:UIControlStateNormal];
        [eyeBtn setImage:[UIImage imageNamed:@"open_eye"] forState:UIControlStateSelected];
        _pwdTF.rightViewMode = UITextFieldViewModeAlways;
        _pwdTF.rightView = eyeBtn;
        _pwdTF.keyboardType = UIKeyboardTypeASCIICapable;
        _pwdTF.returnKeyType = UIReturnKeyDone;
        [_pwdTF setValue:@16 forKey:Limit_NAME];
    }
    return _pwdTF;
}
- (XThemeFlatButton*)loginBtn{
    if (!_loginBtn) {
        _loginBtn = [XThemeFlatButton button];
        [_loginBtn setTitle:XLocalized(@"登录") forState:UIControlStateNormal];
    }
    return _loginBtn;
}

- (UserServer*)userServer{
    if (!_userServer) {
        _userServer = [UserServer new];
    }
    return _userServer;
}
@end
